import Qt 4.7

Rectangle {
    id: container

    width: 525; height: 204
    color: "transparent"

    property string mode: "Add"
    property int indexGoal: -1
    property string today: daoManager.today()

    signal callMessageBox(string mesgBox, string nextView)

    function enableEditMode(index) {
        container.mode = "Edit"
        var listGoals = daoManager.getAllGoals();
        if (index >= 0 && index < listGoals.length) {
            var goal = listGoals[index];
            var typeGoal = daoManager.getAllTypeGoal();
            if (goal.goalType >= 0 && goal.goalType < typeGoal.length) {
                typeDropDown.idSelected = goal.goalType;
                typeDropDown.textSelected = typeGoal[goal.goalType].name;
            }
            valueLineEdit.text = goal.target;
            beginDateInput.text = goal.initialDate;
            endDateInput.text = goal.finalDate;
            nameLineEdit.text = goal.description;
            container.indexGoal = index;
        }
    }
    function eraseFields() {
        valueLineEdit.text = 0;
        beginDateInput.text = container.today;
        endDateInput.text = container.today;
        nameLineEdit.text = "";
    }

    Text {
        id: addTargetLabel

        x: 39; y: -20
        text: "Adicionar nova Meta"; color: "#00adef"
        font { pointSize: 9; weight: Font.DemiBold; family: "Series 60 Sans" }
    }
    Text {
        id: typeLabel

        x: 71; y: 23
        text: "Tipo"; color: "black"
        font { pointSize: 7; weight: Font.DemiBold; family: "Series 60 Sans" }
    }
    Text {
        id: beginLabel

        x: 71; y: 69
        text: "Inicio"; color: "black"
        font { pointSize: 7; weight: Font.DemiBold; family: "Series 60 Sans" }
    }
    DateInput {
        id: beginDateInput

        x: 141; y: 59;
        text: daoManager.today();
    }
    Text {
        id: endLabel

        x: 267; y: 69
        text: "Fim"; color: "black"
        font { pointSize: 7; weight: Font.DemiBold; family: "Series 60 Sans" }
    }
    DateInput {
        id: endDateInput

        x: 311; y: 59;
        text: daoManager.today();
    }
    Text {
        id: valueLabel

        x: 71; y: 119
        text: "Valor"; color: "black"
        font { pointSize: 7; weight: Font.DemiBold; family: "Series 60 Sans" }
    }
    LineEdit {
        id: valueLineEdit

        x: 141; y: 107; width: 174; height: 53
    }
    Text {
        id: nameLabel

        x: 71; y: 188
        text: "Nome"; color: "black"
        font { pointSize: 7; weight: Font.DemiBold; family: "Series 60 Sans" }
    }
    LineEdit {
        id: nameLineEdit

        x: 141; y: 176; width: 174; height: 53
    }
    Button {
        id: okButton

        x: 382; y: 123; width: 101; height: 50
        text: "OK"

        onClicked: {
            // database-related code here
            if (container.mode == "Edit") {
                daoManager.updateGoal(typeDropDown.idSelected, valueLineEdit.text,
                               beginDateInput.text, endDateInput.text, nameLineEdit.text, container.indexGoal);
                container.callMessageBox("Meta modificada!", "TargetsView");
            } else {
                daoManager.addGoal(typeDropDown.idSelected, valueLineEdit.text,
                               beginDateInput.text, endDateInput.text, nameLineEdit.text);
                container.callMessageBox("Meta adicionada", "TargetsView");
            }
            container.eraseFields();

        }
    }
    AdvancedDropDown {
        id: typeDropDown

        x: 121; y: 9; width: 174; height: 42
        color: "transparent";
        model: daoManager.getAllTypeGoal()
        allowCreate: false
        internalWidth: 174; normalWidth: 174; extendedWidth: 174
        textSelected: "Total"

        onItemSelected: {
            // code needed
        }
    }
}
